<?php
/*
 * ebiz front
 *
 * SearchController
 *
 * @author allen
 *
 */
//require_once 'BaseController.php';
require_once '../app/core/controllers/BaseController.php';
require_once '../app/constants/ConfigConstants.php';
require_once '../app/services/CategoryService.php';
require_once '../app/services/ProductService.php';
class SearchController extends BaseController {

    public function index($param = array()){
    	array_shift($param);
        array_shift($param);
     	$keywords = '';
        $int_nowpage = 1;
        $cid = 0;
        $brand = '';
        $paginate = HttpUtil::getPostString('paginate');
       
        if($paginate){
        	$keywords = HttpUtil::getPostString('keywords');
        	$cid = HttpUtil::getPostString('cid');
        	$brand = HttpUtil::getPostString('brand');
        	$int_nowpage = HttpUtil::getPostString('page');
        }else {
            if(!empty($param[0])){
                $keywords = urldecode(trim($param[0]));
            }
          
            if(!empty($param[1])){
        	    if(trim(strtolower($param[1]))=='cid' && !empty($param[2])){
        		    $cid = $param[2];
        	    } else if(trim(strtolower($param[1]))=='brand' && !empty($param[2])){
        		    $brand = urldecode($param[2]);
        	    }
            }
        }
        $lprice = HttpUtil::getPostString('lowestPrice');
        $hprice = HttpUtil::getPostString('highestPrice');
        $priceArr = array();
        array_push($priceArr,$lprice,$hprice);
        
        $categoryService = new CategoryService();
        if($cid > 0){
        	$category = $categoryService->getCategoryById($cid);
        	$cName = $category[0]['short_name'];
        } else {
        	$cName = "";
        }
        
        $productService = new ProductService();
        $allSearchResults = $productService->getAllOfSearch($keywords,$cid,$brand,$lprice,$hprice);
        $gNumArr = $productService->getCategoryPNumOfSearch($keywords,$cid,$brand,$lprice,$hprice,1);
        $cNumArr = $productService->getCategoryPNumOfSearch($keywords,$cid,$brand,$lprice,$hprice,2);
        $bNumArr = $productService->getCategoryPNumOfSearch($keywords,$cid,$brand,$lprice,$hprice,'brand');
        $searchProducts = $productService->getProductsBySearch($keywords,$int_nowpage,$cid,$brand,$lprice,$hprice);
        $result_num = count($allSearchResults);
       
        $groupArr = array();
        $brandArr = array();
        $selectedCategory = array();
        foreach($allSearchResults as $searchProduct ) {;
        	$categoryArr = array();
        	if(!array_key_exists($searchProduct['brand'],$brandArr)){
        		$brandArr[$searchProduct['brand']] = $searchProduct['brand'];
        	}
            if(!array_key_exists($searchProduct['category_id_1'],$groupArr)){
        		$category_1 = $categoryService->getCategoryById($searchProduct['category_id_1']);
        		$groupArr[$searchProduct['category_id_1']] = array();
        		$groupArr[$searchProduct['category_id_1']]['name'] = $category_1[0]['short_name'];
        	    $category_2 = $categoryService->getCategoryById($searchProduct['category_id_2']);
        	
        		$groupArr[$searchProduct['category_id_1']]['category'] = array();
        		$groupArr[$searchProduct['category_id_1']]['category'][$searchProduct['category_id_2']]['title'] = $category_2[0]['short_name'];
        	} else {
            	if(!array_key_exists($searchProduct['category_id_2'],$groupArr[$searchProduct['category_id_1']]['category'])){
            	     $category_2 = $categoryService->getCategoryById($searchProduct['category_id_2']);
            		 $groupArr[$searchProduct['category_id_1']]['category'][$searchProduct['category_id_2']]['title'] = $category_2[0]['short_name'];
            		 $groupArr[$searchProduct['category_id_1']]['category'][$searchProduct['category_id_2']]['p_num'] = 1;
            	}
           }
        }
      
        require_once '../app/util/Pager.php';
        $pager = new Pager();
        $pager->setJavascriptFunction("javascript:Search.resultPaginate");
        $itemsPerPage = $int_pagesize = Pager::GetDefaultPagesize();//akimimi set is 20 
        $headerPaginate = $pager->paginate($int_nowpage, $result_num, "", $itemsPerPage);
      
        require_once '../app/views/SearchResultView.php';   
        $searchResultView = new SearchResultView();
        echo $searchResultView->getPage($groupArr,$brandArr,$searchProducts,$gNumArr,$cNumArr,$bNumArr,$result_num,$headerPaginate,$keywords,$cid,$cName,$brand,$priceArr,$paginate);
        exit(); 	
    }
    
}



